<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;

class UserController extends Controller
{
    public function add()
    {
    	return view('admin.user.add');
    } 

    public  function insert(Request $request)
    {
    	$this -> validate($request,[
    		'name' => 'required|min:1|max:18',
    		'password' => 'required',
    		'repassword' =>'required|same:password',
    		'email' => 'email',
    		'email' => 'required',
    		],[
    		'name.required' => '用户名不能为空',
    		'name.min' => '用户名不能少于6位',
    		'name.max' => '用户名不能大于18位',
    		'password.required' => '密码不能为空',
    		'repassword.required' => '确认密码不能为空',
    		'repassword.same' => '两次密码不一致',
    		'email.email' => '邮箱不合法',
    		'email.required' => '邮箱不能为空',
    		]);

    	$data = $request -> except('_token','repassword');

    	//处理密码
    	$data['password'] = Crypt::encrypt($data['password']);

    	// 处理token
    	$data['token'] = str_random(50);

    	//状态
    	$data['status'] = 1;

    	//执行添加
    	$res = DB::table('users') -> insert($data);

    	//判断
    	if($res)
    	{
    		return redirect('/admin/user/index') ->with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}

    	

    }

    public function index(Request $request)
    {
    	$data = DB::table('users')
    	 ->where('name','like','%'.$request -> input('keywords').'%')
         ->orderBy('id') 
    	 ->paginate($request ->input('num',10));

    	

    	return view('admin/user/index',['data' => $data,'request' => $request ->all()]);
    }

    public function ajaxUpdate(Request $request)
    {	
    	//获取id
    	$id = $request ->input('id');

    	//查询这条id的状态
    	$data = DB::table('users') ->select('status') ->where('id',$id) ->first();

    	//根据他的状态修改数据库的状态值取反
    	if($data -> status == 0)
    	{
    		$res = DB::table('users')-> where('id',$id)->update(['status'=>1]);
    		if ($res) {
    			return response() -> json(1);
    		}else
    		{
    			// 5代表失败
    			return response() ->json(5);
    		}

    		
    	}else
    	{
    		$res = DB::table('users') -> where('id',$id) ->update(['status'=>0]);	
    		if ($res) {
    			return response() -> json(0);
    		}else
    		{
    			//5代表失败
    			return response() ->json(5);
    		}
    	}

    }

    public function ajaxName(Request $request)
    {
    	// $data = $request ->all();
    	// dd($data);
    	//根据获取的name值和id值去判断数据库是否存在这个名称
    	$res = DB::table('users') -> where('name',$request -> input('name'))
    	->first();

    	if($res)
    	{
    		// 存在这个名称
    		return response() -> json(5);
    	}else
    	{
    		$r = DB::table('users') -> where('id',$request->id)
    		->update(['name' => $request -> name]);

    		//判断是否修改成功
    		if($r)
    		{
    			//修改成功
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(1);
    		}
    	}

    }

    public function edit($id)
    {
    	$data = DB::table('users') ->where('id',$id) ->first();
    	return view('admin.user.edit',['data' => $data]);
    }

    public function update(Request $request)
    {
    	$data = $request -> except('_token');
    	$res = DB::table('users') -> where('id',$data['id']) -> update($data);
    	if($res)
    	{
    		return redirect('/admin/user/index') ->with(['info' => '修改成功']);
    	}else
    	{
    		return back() ->with(['info' => '修改失败']);
    	}
    }

    public function delete($id)
    {
    	$res = DB::table('users') ->delete($id);
    	if($res)
    	{
    		return redirect('/admin/user/index') ->with(['info' => '删除成功']);
    	}else
    	{
    		return back() ->with(['info' => '删除失败']);
    	}
    }
}
